home *** CD-ROM | disk | FTP | other *** search
/ Apple Developer Connection Student Program / ADC Tools Sampler CD Disk 3 1999.iso / Metrowerks CodeWarrior / Java Support / Java_Source / Java2 / src / java / lang / InheritableThreadLocal.java < prev    next >
Encoding:
Java Source  |  1999-05-28  |  3.6 KB  |  109 lines  |  [TEXT/CWIE]

  1. /*
  2.  * @(#)InheritableThreadLocal.java    1.2 98/06/29
  3.  *
  4.  * Copyright 1998 by Sun Microsystems, Inc.,
  5.  * 901 San Antonio Road, Palo Alto, California, 94303, U.S.A.
  6.  * All rights reserved.
  7.  *
  8.  * This software is the confidential and proprietary information
  9.  * of Sun Microsystems, Inc. ("Confidential Information").  You
  10.  * shall not disclose such Confidential Information and shall use
  11.  * it only in accordance with the terms of the license agreement
  12.  * you entered into with Sun.
  13.  */
  14.  
  15. package java.lang;
  16.  
  17. /**
  18.  * This class extends ThreadLocal to provide inheritance of values from parent
  19.  * Thread to child Thread: when a child thread is created, the child receives
  20.  * initial values for all InheritableThreadLocals for which the parent has
  21.  * values.  Normally the child's values will be identical to the parent's;
  22.  * however, the child's value can be made an arbitrary function of the parent's
  23.  * by overriding the childValue method in this class.
  24.  * <p>
  25.  * InheritableThreadLocal variables are used in preference to ordinary
  26.  * ThreadLocal variables when the per-thread-attribute being maintained in the
  27.  * variable (e.g., User ID, Transaction ID) must be automatically transmitted
  28.  * to any child threads that are created.
  29.  *
  30.  * @author  Josh Bloch
  31.  * @version 1.2 06/29/98
  32.  * @see ThreadLocal
  33.  * @since JDK1.2
  34.  */
  35.  
  36. public class InheritableThreadLocal extends ThreadLocal {
  37.     /**
  38.      * Creates an InheritableThreadLocal variable.
  39.      */
  40.     public InheritableThreadLocal() {
  41.     }
  42.  
  43.     /**
  44.      * Computes the child's initial value for this InheritableThreadLocal
  45.      * as a function of the parent's value at the time the child Thread is
  46.      * created.  This method is called from within the parent thread before
  47.      * the child is started.
  48.      * <p>
  49.      * This method merely returns its input argument, and should be overridden
  50.      * if a different behavior is desired.
  51.      */
  52.     protected Object childValue(Object parentValue) {
  53.         return parentValue;
  54.     }
  55.  
  56.     /**
  57.      * Passes the ThreadLocal values represented by the specified list of
  58.      * Entries onto the specified child Thread.  (The child's value is
  59.      * computed from the parent's as per the childValue method.)  This
  60.      * method is invoked (only) at Thread creation time, by Thread.init.
  61.      */
  62.     static void bequeath(Thread parent, Thread child) {
  63.         for (Entry e = parent.values; e != null; e = e.next)
  64.             e.bequeath(child);
  65.     }
  66.  
  67.     /**
  68.      * Overrides method in ThreadLocal and implements inheritability,
  69.      * in conjunction with the bequeath method.
  70.      */
  71.     ThreadLocal.Entry newEntry(Object value) {
  72.         return new Entry(value);
  73.     }
  74.  
  75.     /**
  76.      * The information associated with an (InheritableThreadLocal,Thread) pair.
  77.      */
  78.     class Entry extends ThreadLocal.Entry {
  79.         /**
  80.          * This constructor places the newly constructed Entry on the
  81.          * specified thread's values list.
  82.          */
  83.         private Entry(Object value, Thread t) {
  84.             super(value);
  85.             next = t.values;
  86.             t.values = this;
  87.         }
  88.  
  89.         /**
  90.          * This constructor places the newly constructed Entry on the
  91.          * calling thread's values list.
  92.          */
  93.         Entry(Object value) {
  94.             this(value, Thread.currentThread());
  95.         }
  96.  
  97.         /**
  98.          * Passes the ThreadLocal value represented by this Entry on to the
  99.          * specified child Thread.
  100.          */
  101.         void bequeath(Thread child) {
  102.             Entry e = new Entry(childValue(value), child);
  103.             map.put(child, e);
  104.         }
  105.  
  106.         Entry  next;    // Allows entries to be linked onto a list
  107.     }
  108. }
  109.